import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext import search
from google.appengine.ext import db
import json

class MainPage(webapp.RequestHandler):
  def get(self):
    # We use the webapp framework to retrieve the postal_code
    postal_code = self.request.get('postal_code')

    self.response.headers['Content-Type'] = 'text/html'
    if not postal_code:
      self.response.out.write("<p>No postal_code has been set</p>")
    else:
      # Search the 'PostalCode' Entity based on our postal_code
      query = search.SearchableQuery('PostalCode')
      query.Search(postal_code)
      for result in query.Run():
         self.response.out.write('<p>'+result['suburb']+'</p>')

def main():
  application = webapp.WSGIApplication([('/find_post_codes', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()